package com.dotflat.physicalevaluate.controller;

import com.matt.commons.dto.sso.StaffDTO;
import com.matt.foundation.model.ErrorCode;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.service.UploadService;
import com.tuo51.foudation.model.StatusResponse;
import com.tuo51.foudation.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;

import static com.matt.foundation.constant.RequestConstant.TICKET;

@RestController
@RequestMapping("/peapi/upload")
@Api(tags = "UP - 文件上传")
public class UploadController {

    @Autowired
    private UploadService uploadService;

    @Autowired
    private MemVerificationCache cache;

    @ApiOperation(value = "上传图片，每天一个文件夹")
    @RequestMapping(value = "/file" ,method = RequestMethod.POST)
    @ResponseBody
    public StatusResponse uploadFile(@RequestHeader(TICKET)String ticket, String bizType, MultipartFile file) {
        if (Objects.isNull(file))
            return StatusResponse.error(ErrorCode.FILE_MAST_NOT_NULL,"文件不能为空");

        StaffDTO staff = cache.getStaff(ticket);

        String rel = DateUtil.getToday();

        if (StringUtils.isNotEmpty(bizType)){
            if (bizType.length() < 3 || bizType.length() > 20 || bizType.contains("/")) {
                return StatusResponse.error(ErrorCode.PARAM_CAN_NOT_BLANK, "参数(或长度)错误");
            }

            rel = bizType + "/" + rel;
        }

        String url = uploadService.uploadCommonFile(staff.getId(), rel, file);

        return StatusResponse.success(url);
    }

}
